package com.dotstone.chipism.adapter;

import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.DeviceType;
import com.dotstone.chipism.bean.ModelSort;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.Util;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class ModelAdapter extends BaseAdapter implements SectionIndexer{

	private Context context;
	private List<ModelSort> data;
	private Handler mHandler;
	private String appliance;
	private int type;
	private String brand;

	public ModelAdapter(Context context) {
		this.context = context;
	}
	
	public List<ModelSort> getAdapterData(){
		return data;
	}

	public ModelAdapter(Context context, List<ModelSort> data,String brand,int type) {
		this.data = data;
		this.context = context;
		this.brand = brand;
		this.type = type;
		appliance = Util.getAppliance(context, type);
	}

	public void setData(List<ModelSort> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void updateListView(List<ModelSort> data){
		this.data = data;
		notifyDataSetChanged();
	}

	public void clear() {
		this.data.clear();
		notifyDataSetChanged();
	}

	public void addAll(List<ModelSort> data) {
		if (data != null) {
			this.data.addAll(data);
			notifyDataSetChanged();
		}
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.item_model, null);
			holdView = new HoldView();
			holdView.applianceTv = (TextView) view.findViewById(R.id.name_tv);
			holdView.modelTv = (TextView) view.findViewById(R.id.model_tv);
			holdView.mTypeImg = (ImageView)view.findViewById(R.id.type_img);
			holdView.addTv = (TextView)view.findViewById(R.id.add_tv);
			view.setTag(holdView);
		} else {
			holdView = (HoldView) view.getTag();
		}
		ModelSort r = data.get(position);
		holdView.applianceTv.setText(brand+" "+appliance);
		holdView.modelTv.setText(r.getModel());
		boolean e = DeviceManager.getInstance().checkName(appliance + " " + brand + " " + r.getModel());
		if(e){
			//该名字的遥控器已经存在
			holdView.addTv.setVisibility(View.VISIBLE);
		}
		switch (type) {
		case DeviceType.DEVICE_TYPE_AIR_CONTROLLER:
			holdView.mTypeImg.setImageResource(R.drawable.machine_air_tag);
			break;
		case DeviceType.DEVICE_TYPE_TV:
			holdView.mTypeImg.setImageResource(R.drawable.machine_tv_tag);
			break;
		case DeviceType.DEVICE_TYPE_FAN:
			holdView.mTypeImg.setImageResource(R.drawable.machine_fan_tag);
			break;
		case DeviceType.DEVICE_TYPE_PROJECTOR:
			holdView.mTypeImg.setImageResource(R.drawable.machine_projector_tag);
			break;
		case DeviceType.DEVICE_TYPE_SATELLITE:
			holdView.mTypeImg.setImageResource(R.drawable.machine_stb_tag);
			break;
		case DeviceType.DEVICE_TYPE_DVD:
			holdView.mTypeImg.setImageResource(R.drawable.machine_dvd_tag);
			break;
		case DeviceType.DEVICE_TYPE_CAMERA:
			holdView.mTypeImg.setImageResource(R.drawable.machine_camera_tag);
			break;
		case DeviceType.DEVICE_TYPE_DENG:
			holdView.mTypeImg.setImageResource(R.drawable.machine_ir_switch);
			break;
		case DeviceType.DEVICE_TYPE_AUDIO:
			holdView.mTypeImg.setImageResource(R.drawable.machine_amplifier_tag);
			break;
		case DeviceType.DEVICE_TYPE_IPTV:
			holdView.mTypeImg.setImageResource(R.drawable.machine_iptv_tag);
			break;
		case DeviceType.DEVICE_TYPE_BOX2:
			holdView.mTypeImg.setImageResource(R.drawable.machine_wifibox);
			break;

		default:
			break;
		}
		
		return view;
	}

	HoldView holdView;

	class HoldView {
		private TextView applianceTv,modelTv,addTv;
		private ImageView mTypeImg;
	}

	public Object[] getSections() {
		return null;
	}

	/**
	 * 根据ListView的当前位置获取分类的首字母的Char ascii值
	 */
	public int getSectionForPosition(int position) {
		return data.get(position).getSortLetters().charAt(0);
	}

	/**
	 * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
	 */
	public int getPositionForSection(int section) {
		for (int i = 0; i < getCount(); i++) {
			String sortStr = data.get(i).getSortLetters();
			char firstChar = sortStr.toUpperCase().charAt(0);
			if (firstChar == section) {
				return i;
			}
		}
		
		return -1;
	}

}

